<script setup>
// 图片列表
import {ref, watch} from "vue";
import {useMouseInElement} from "@vueuse/core";

//写成Props,这样就成了组件可以给别人用
const props = defineProps({
  imageList: {
    type: Array,
    default: () => [],
  }
});

const imageList = props.imageList;
const activeIndex = ref(0);

//mouseenter表示如果有鼠标进入只会，会触发
const changeIndex = (i) => {
  activeIndex.value = i;
}

//参数target就是判断鼠标是否在那个target里面以及相对值
const target = ref(null)
//div里面用ref进行赋值，elementX,elementY都是相对距离
const {elementX, elementY, isOutside} = useMouseInElement(target);

//监控x,y的变化，然后改变蒙层小滑块的位置
const layerX = ref(0);
const layerY = ref(0);

//放大图的变化
const positionX = ref(0);
const positionY = ref(0);
watch([elementX, elementY], () => {

  //在外面就不执行
  if (isOutside.value) {
    return;
  }
  //外面的大正方形是400px 400px
  let w = 400, h = 400;
  //里面的小正方形是200px,200px
  let sw = 200, sh = 200;

  //[x1,x2]内是可以滑动的，小于x1或者大于x2就固定在最左或者最右
  let x1 = (sw / 2), x2 = w - (sw / 2);
  //[y1,y2]内是可以滑动的，小于y1或者大于y2就固定在最上或者最下
  let y1 = (sh / 2), y2 = h - (sh / 2);

  if (elementX.value >= x1 && elementX.value <= x2) {
    layerX.value = elementX.value - (sw / 2);
  } else if (elementX.value < x1) {
    layerX.value = 0;
  } else if (elementX.value > x2) {
    layerX.value = w - sw;
  }

  if (elementY.value >= y1 && elementY.value <= y2) {
    layerY.value = elementY.value - (sh / 2);
  } else if (elementY.value < y1) {
    layerY.value = 0;
  } else if (elementY.value > y2) {
    layerY.value = h - sh;
  }

  //大图效果,反向移动（如果无法想象，可以把负号去掉）
  positionX.value = -2 * layerX.value;
  positionY.value = -2 * layerY.value;
})
</script>


<template>
  {{ elementX }},{{ elementY }},{{ isOutside }}
  <div class="goods-image">
    <!-- 左侧大图-->
    <div class="middle" ref="target" >
      <img :src="imageList[activeIndex]" alt=""/>
      <!-- 蒙层小滑块,使用:+$动态top,left -->
      <div class="layer" :style="{ left: `${layerX}px`, top: `${layerY}px` }" v-show="!isOutside"></div>
    </div>
    <!-- 小图列表 -->
    <ul class="small">
      <li v-for="(img, i) in imageList" :key="i" @mouseenter="changeIndex(i);" :class="{ active:(i===activeIndex)}">
        <img :src="img" alt=""/>
      </li>
    </ul>
    <!-- 放大镜大图 -->
    <div class="large" :style="[
      {
        backgroundImage: `url(${imageList[activeIndex]})`,
        backgroundPositionX: `${positionX}px`,
        backgroundPositionY: `${positionY}px`,
      },
    ]" v-show="!isOutside"></div>
  </div>
</template>

<style scoped lang="scss">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;

  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
  }

  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    // 背景图:盒子的大小 = 2:1  将来控制背景图的移动来实现放大的效果查看 background-position
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  .layer {
    width: 200px;
    height: 200px;
    background: rgba(0, 0, 0, 0.2);
    // 绝对定位 然后跟随咱们鼠标控制left和top属性就可以让滑块移动起来
    left: 0;
    top: 0;
    position: absolute;
  }

  .small {
    width: 80px;

    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;

      &:hover,
      &.active {
        border: 2px solid $xtxColor;
      }
    }
  }
}
</style>